from selenium.webdriver.common.by import By
from webdriver_helper import get_webdriver
from webdriver_helper.pom import BasePage, LazyElementList
from webdriver_helper.pom import LazyElement


# 类代表是页面
class IndexPage(BasePage):
    """首页"""
    # 属性：代表是元素定位
    ipt_search = LazyElement(By.XPATH, '//*[@id="search-input"]')
    btn_search = LazyElement(By.XPATH, '//*[@id="ai-topsearch"]')

    # 方法：业务操作
    def search(self, wd):
        # 通过对象调用类属性
        self.ipt_search.send_keys(wd)  # 输入：搜索词
        self.btn_search.click()  # 点击 搜索结果
        return SearchPage(self.driver)


class SearchPage(BasePage):
    p_title_list = LazyElementList(By.XPATH, '/html/body/div[4]/div/ul/li/div/a/div/p')

    def get_all_titles(self):
        """将所有搜索的结果商品标题存放到列表"""

        l = []
        for p in self.p_title_list:
            l.append(p.text)
            return l


if __name__ == '__main__':
    driver = get_webdriver()
    driver.get("http://101.34.221.219:8010/")
    index_page = IndexPage(driver)
    search_page = index_page.search('连衣裙')
    title_list = search_page.get_all_titles()
    for title in title_list:
        print(title)
        assert "手机" in title
